package models.weixin;

import helper.GlobalConfig;
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import models.constants.Gender;
import models.merchant.Merchant;
import play.Logger;

import java.util.Date;

/**
 * 微信公众号工具类.
 */
public class WxMpHelper {

    private static WxMpConfigStorage wxMpConfigStorage = null;

    /**
     * 通过CorpApp得到微信配置.
     */
    public static WxMpConfigStorage getWxMpConfigStorage() {
        if (wxMpConfigStorage == null) {
            wxMpConfigStorage = initWxMpConfigStorage();
        }
        return wxMpConfigStorage;
    }

    /**
     * 通过CorpApp得到微信配置.
     */
    public static WxMpConfigStorage getWxMpConfigStorage(Merchant merchant) {
//        if (wxMpConfigStorage == null || !wxMpConfigStorage.getAppId().equals(merchant.weixinData.appId)) {
            Logger.info("初始化 WxMpConfigStorage");
            wxMpConfigStorage = initWxMpConfigStorage(merchant);
//        }
        return wxMpConfigStorage;
    }

    private static WxMpConfigStorage initWxMpConfigStorage(Merchant merchant) {
        WxMpInMemoryConfigStorage wxMpConfigStorage = new WxMpInMemoryConfigStorage();
        wxMpConfigStorage.setAppId(merchant.weixinData.appId);     // 设置微信公众号的appid
        wxMpConfigStorage.setSecret(merchant.weixinData.appSecure);    // 设置微信公众号的app corpSecret
        wxMpConfigStorage.setToken(merchant.weixinData.appToken);       // 设置微信公众号的token
        wxMpConfigStorage.setAesKey(merchant.weixinData.appAesKey);      // 设置微信公众号的EncodingAESKey
        return wxMpConfigStorage;
    }

    private static WxMpConfigStorage initWxMpConfigStorage() {
        WxMpInMemoryConfigStorage wxMpConfigStorage = new WxMpInMemoryConfigStorage();
        wxMpConfigStorage.setAppId(GlobalConfig.WEIXIN_APP_ID);     // 设置微信公众号的appid
        wxMpConfigStorage.setSecret(GlobalConfig.WEIXIN_APP_SECURET);    // 设置微信公众号的app corpSecret
        wxMpConfigStorage.setToken(GlobalConfig.WEIXIN_TOKEN);       // 设置微信公众号的token
        wxMpConfigStorage.setAesKey(GlobalConfig.WEIXIN_AES_KEY);      // 设置微信公众号的EncodingAESKey
        return wxMpConfigStorage;
    }

    /**
     * 通过ConfigStore得到微信服务接口.
     */
    public static WxMpService getWxMpService(WxMpConfigStorage wxMpConfigStorage) {
        WxMpServiceImpl wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxMpConfigStorage);
        return wxMpService;
    }

    /**
     * 通过ConfigStore得到微信服务接口.
     */
    public static WxMpService getWxMpService() {
        WxMpServiceImpl wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(getWxMpConfigStorage());
        return wxMpService;
    }

//    /**
//     * 转换wxMpUser为User.
//     */
//    public static WebUser createOrUpdateUserFromWxMpUser(Merchant merchant, WxMpUser wxMpUser) throws Exception {
//        WebUser user = WebUser.findOrCreateMerchantWxUser(merchant, wxMpUser);
//        user.city = wxMpUser.getCity();
//        user.country = wxMpUser.getCountry();
//        user.language = wxMpUser.getLanguage();
////        if ("1".equals(wxMpUser.getSex()) || "男".equals(wxMpUser.getSex())) {
////            user.sex = Gender.MALE;
////        } else if ("2".equals(wxMpUser.getSex()) || "女".equals(wxMpUser.getSex())) {
////            user.sex = Gender.FEMALE;
////        } else {
////            user.sex = Gender.UNKNOWN;
////        }
//        user.subcribed = wxMpUser.isSubscribe();
////        user.subscribedAt = new Date(); // new Time(wxMpUser.getSubscribeTime()); //CHECK:是不是要乘以1000？
//        user.save();
//        return user;
//    }
//
    /**
     * 转换wxMpUser为User.
     */
    public static WebUser createOrUpdateUserFromWxMpUser(Merchant merchant, WxMpUser wxMpUser , String dcode) throws Exception {
        return WebUser.findOrCreateMerchantWxUser(merchant, wxMpUser , dcode);
    }
//
//
//
//
//
    public static WebUser getUserFromOpenId(String wxOpenId) {
        Logger.info("getUserFromOpenId  WxOpenId : %s" , wxOpenId);
        return WebUser.findByOpenId(wxOpenId);
    }

}
